package com.lgs.xlist.example;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;

import com.lgs.common.widgets.xlist.BaseListAdapter;
import com.lgs.common.widgets.xlist.XListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {
    private BaseListAdapter<Book> mAdapter;//定义适配器
    @BindView(R.id.xlv_data)
    XListView xlvData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initListView();
    }
    private void initListView() {//初始化列表
        mAdapter = new BaseListAdapter<Book>(this, R.layout.list_item_book, BookListItem.class);
        mAdapter.initListView(xlvData, true, true);
        mAdapter.setXListListener(this::clickItem, this::getData);
        getData(0);
    }

    private Boolean clickItem(int type, Book item) {//点击子项
        String name = item.getName();//field 0
        Log.d("", "selected is " + name);
        return false;
    }
    private void getData(int fromPos) {
        //如果fromPos = 0表示从头开始刷新，否则为下拉加载
        List<Book> books = new ArrayList<>();
        //测试数据
        for (int i = fromPos; i < fromPos+mAdapter.numPerTime; i++) {
            Book book1 = new Book( "name" + i);
            books.add(book1);
        }
        updateData( books);
    }
    public void updateData( List<Book> books) {
        //获取数据之后，更新列表
        xlvData.setDataUpdating(false);//取消下拉状态
        Log.d("", "~~~获取数据之后~~\n ");
        mAdapter.addData(books);
    }
}